package ${package.Controller};

import org.springframework.web.bind.annotation.RequestMapping;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.circle.rest.core.common.page.PageInfoBT;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import ${cfg.wrapperPackage}.${entity}Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import java.time.LocalDateTime;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Api(value = "${table.comment!}", tags = {"<#if package.ModuleName??>${package.ModuleName} : </#if>${table.comment!}API"})
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${table.serviceName?substring(1)?uncap_first};

    /**
     * 查询${table.comment!}列表
     */
    @ApiOperation("查询${table.comment!}列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int"),
            @ApiImplicitParam(name = "condition", value = "关键字", dataType = "String")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Log(title = "查询${table.comment!}列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(Integer index, Integer size, String condition) {
        Page<${entity}> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
        IPage<Map<String, Object>> mapIPage = ${table.serviceName?substring(1)?uncap_first}.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new ${entity}Wrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 添加${table.comment!}
     */
    @ApiOperation("添加${table.comment!}")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Log(title = "添加${table.comment!}", businessType = BusinessType.INSERT)
    @Permission
    public Object add(${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}.setUpdateTime(LocalDateTime.now());
        ${entity?uncap_first}.setCreateTime(LocalDateTime.now());
        boolean save = ${table.serviceName?substring(1)?uncap_first}.save(${entity?uncap_first});
        return new SuccessResponseData(save);
    }

    /**
     * 修改${table.comment!}
     */
    @ApiOperation("修改${table.comment!}")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Log(title = "修改${table.comment!}", businessType = BusinessType.UPDATE)
    @Permission
    public Object edit(${entity} ${entity?uncap_first}) {
        if (ToolUtil.isEmpty(${entity?uncap_first}.getId())) {
            throw new ServiceException("编号不能为空");
        }
        if (ToolUtil.isEmpty(${entity?uncap_first}.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        ${entity?uncap_first}.setUpdateTime(LocalDateTime.now());
        if (!${table.serviceName?substring(1)?uncap_first}.updateById(${entity?uncap_first})) {
            throw new ServiceException("更新失败(版本号不一致)");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除${table.comment!}
     */
    @ApiOperation("删除${table.comment!}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "${table.comment!}编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @Log(title = "删除${table.comment!}", businessType = BusinessType.DELETE)
    @Permission
    public Object delete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("${table.comment!}编号不能为空");
        }
        ${entity} ${entity?uncap_first}Old = ${table.serviceName?substring(1)?uncap_first}.getById(id);
        if (ToolUtil.isEmpty(${entity?uncap_first}Old)) {
            throw new ServiceException("${table.comment!}不存在，删除失败");
        }
        if (!${table.serviceName?substring(1)?uncap_first}.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询${table.comment!}
     */
    @ApiOperation("查询${table.comment!}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "${table.comment!}编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询${table.comment!}", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("${table.comment!}编号不能为空");
        }
        ${entity} ${entity?uncap_first} = ${table.serviceName?substring(1)?uncap_first}.getById(id);
        if (ToolUtil.isEmpty(${entity?uncap_first})) {
            throw new ServiceException("${table.comment!}不存在");
        }
        return new SuccessResponseData(${entity?uncap_first});
    }
}
</#if>
